package com.socialrss.adapters;

import java.util.ArrayList;

import com.socialrss.R;
import com.socialrss.viewlisteners.AbstractManageListListener;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.WindowManager.LayoutParams;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;

/**
 * 
 * @author shnkc
 *
 *This class implements onClickListener. When relevant item is clicked, a popup window appears.
 *List item is string with a checkbox.
 */
public class SelectTypeClickListener implements OnClickListener{
	private LayoutInflater thislayoutInf;
	private ArrayList<StringWithStatus> conntype;
	private Context con;
	private int relativeViewId;
	
	public SelectTypeClickListener(Context con, LayoutInflater lay, ArrayList<StringWithStatus> conntypes, int relativeViewId){
		thislayoutInf = lay;
		this.conntype = conntypes;
		this.con = con;
		this.relativeViewId = relativeViewId;
	}
	@Override
	public void onClick(View arg0) {
		View popupView = thislayoutInf.inflate(R.layout.conn_type_popup, null);
		final PopupWindow conntypepopup = new PopupWindow(popupView,LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
		
		//Specify list view
		ListView conntypelistview = (ListView) popupView.findViewById(R.id.conntypelistviewinpopup);
		conntypelistview.setAdapter(new StringArrayAdapter(con, R.layout.conntype_list_item, conntype));
		
		//Specify confirm button
		View confirmbutton = popupView.findViewById(R.id.conntypepopupconfirmbutton);
		confirmbutton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				conntypepopup.dismiss();
				((AbstractManageListListener)SelectTypeClickListener.this.con).adjustButtonListener_setConnList();
			}
		});
		conntypepopup.setFocusable(true);
		conntypepopup.showAsDropDown(((Activity)con).findViewById(relativeViewId),100,25);
	} 
	//Array Adapter for a list item which has a string and a checkbox
	private class StringArrayAdapter extends ArrayAdapter<StringWithStatus> {
		 
		  private ArrayList<StringWithStatus> connectionList;
		  
		 
		  public StringArrayAdapter(Context context, int textViewResourceId, ArrayList<StringWithStatus> connTypeList) {
			  super(context, textViewResourceId, connTypeList);
			  this.connectionList = connTypeList;
		  }
		  private class ViewHolder {
			  TextView name;
			  CheckBox sel;
		  }
		  @Override
		  public View getView(int posi, View convertView, ViewGroup parent) {

			  ViewHolder holder = null;
			  Log.v("ConvertView", String.valueOf(posi));
			  final int position = posi;
		 
			  if (convertView == null) {
				  LayoutInflater vi = (LayoutInflater)(((Activity)this.getContext()).getSystemService(Context.LAYOUT_INFLATER_SERVICE));
				  convertView = vi.inflate(R.layout.conntype_list_item, null);
			 
				  holder = new ViewHolder();
				  holder.name = (TextView) convertView.findViewById(R.id.connTypeTextViewInListItem);
				  holder.sel = (CheckBox) convertView.findViewById(R.id.connTypeCheckboxInListItem);
				  convertView.setTag(holder);
			  } 
			  else {
				  holder = (ViewHolder) convertView.getTag();
			  }
			  
			  holder.sel.setOnCheckedChangeListener(new OnCheckedChangeListener() {
					@Override
					public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
							connectionList.get(position).setStatus(arg1);
						}
				  	});
			  
			  holder.name.setText(connectionList.get(position).getName());
			  holder.sel.setChecked(connectionList.get(position).getStatus());
			  holder.sel.setTag(connectionList.get(position));
			  return convertView;
		 
		  }
	}
}
